package com.example.test333;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 登录
     */
    private Button mBt;
    /**
     * 分享
     */
    private Button mFx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
    private void initView() {
        mBt = (Button) findViewById(R.id.bt);
        mBt.setOnClickListener(this);
        mFx = (Button) findViewById(R.id.fx);
        mFx.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt:
                  UMShareAPI.get(this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                                  @Override
                                  public void onStart(SHARE_MEDIA share_media) {

                                  }

                                  @Override
                                  public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                                      for (String key : map.keySet()) {
                                          Log.e("liangxq", "key=" + key + "value=" + map.get(key));
                                      }
                                  }

                                  @Override
                                  public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                                      Log.e("liangxq", throwable.getMessage());
                                  }

                                  @Override
                                  public void onCancel(SHARE_MEDIA share_media, int i) {

                                  }
                              });
                break;
            case R.id.fx:
                  UMImage image = new UMImage(MainActivity.this, R.mipmap.ic_launcher);//网络图片
                                  new ShareAction(MainActivity.this).withText("hello").withMedia(image).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                                          .setCallback(new UMShareListener() {
                                              @Override
                                              public void onStart(SHARE_MEDIA share_media) {

                                              }

                                              @Override
                                              public void onResult(SHARE_MEDIA share_media) {
                                                  Log.e("liangxq", "onResult");
                                              }

                                              @Override
                                              public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                                  Log.e("liangxq", throwable.getMessage());
                                              }

                                              @Override
                                              public void onCancel(SHARE_MEDIA share_media) {

                                              }
                                          }).open();
                break;
        }
    }
}
